﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Security.Cryptography;

namespace ExcelAddIn.Common
{
    public class Md5Helper
    {
        #region "MD5加密"

        /// <summary>
        /// MD5加密
        /// </summary>
        /// <param name="str">加密字符</param>
        /// <param name="code">加密位数16/32</param>
        /// <returns></returns>
        public static string GetMd5Hash(string str)
        {
            string strEncrypt = string.Empty;
            MD5 md5 = new MD5CryptoServiceProvider();
            byte[] result = Encoding.Default.GetBytes(str); 
            byte[] output = md5.ComputeHash(result);
            strEncrypt = BitConverter.ToString(output).Replace("-", "");
            return strEncrypt;
        }

        /// <summary>
        ///  验证hash值
        /// </summary>
        /// <param name="input">字符串</param>
        /// <param name="hash">hash值</param>
        /// <returns></returns>
        public static bool VerifyMd5Hash(string input, string hash)
        {
            string hashOfInput = GetMd5Hash(input);
            StringComparer comparer = StringComparer.OrdinalIgnoreCase;

            if (0 == comparer.Compare(hashOfInput, hash))
            {
                return true;
            }
            else
            {
                return false;
            }
        }

        #endregion "MD5加密"
    }
}
